﻿Public Class MuhMahsupGiris : Inherits Process

	Dim WithEvents Grid As Grid
	Dim yil, fisno As Input

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Mahsup fişi girişi", "Fiş numarasını girin.")
				yil = AddText("yil", "Yıl", "width=4", Today.Year, "Yılı girmeniz gerekir")
				fisno = AddText("fisno", "Fiş no.", "width=4;nl", "", "Fiş bilgisini girmeniz gerekir")
				StartBody()
				AddList()
				Grid = List
				List.AddColumns("*hesapno(Hesap no.;80c),hesapadi(Hesap adı;50%),*fisacik(Açıklama;50%),*borc(Borç;80m),*alacak(Alacak;80m)")
			Case EventType.Shown
				Grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
				Grid.DefaultCellStyle.Padding = New Padding(3, 2, 3, 2)
				Grid.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
				Grid.ShowLineNumbers = True
				Grid.HideZeros = True
				Buttons.Show("Import")
			Case EventType.Import
				ShowDialog()
			Case EventType.ValidateForm
				Dim Rs = Muhasebe.Mahsup.FisOku(Remote, yil.Get, fisno.Get)
				If Check("yil", Rs.EOF, "!Fiş numarası bulunamadı.") Then Return
				List.DataSource = Rs
				For Each Row As DataGridViewRow In Grid.Rows
					ShowResults("hesapno", Row.Cells("hesapno").Value, Row)
				Next
				Buttons.Show("Save.Reset")
				e.Result = True
		End Select
	End Sub

	Function AddRow() As DataGridViewRow
		Grid.Rows.Add()
		Grid.CurrentCell = Grid.Rows(Grid.Rows.Count - 1).Cells("hesapno")
		Return Grid.Rows(Grid.Rows.Count - 1)
	End Function

	Function ShowResults(tur As String, Value As Object, Row As DataGridViewRow) As Boolean
		Row.Cells("hesapadi").Value = Muhasebe.HesapAdi(Remote, Value)
	End Function

	Private Sub Grid_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles Grid.CellValidating
		Dim Row = Grid.Rows(Grid.CurrentCell.RowIndex)
		Dim Col = Grid.Columns(Grid.CurrentCell.ColumnIndex)
		Dim Cell = Row.Cells(Col.Name)
		If Not Grid.IsCurrentCellDirty Then Return
		Select Case Col.Name
			Case "hesapno"
				ShowResults("hesapno", Grid.EditingControl.Text, Row)
		End Select
	End Sub

	Private Sub Grid_Propegate(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Grid.Propegate
		e.Handled = True
		Dim Row = Grid.Rows(Grid.CurrentCell.RowIndex)
		Dim Col = Grid.Columns(Grid.CurrentCell.ColumnIndex)
		Dim Cell = Row.Cells(Col.Name)
		Select Case Col.Name
			Case "hesapno" : Grid.CurrentCell = Row.Cells("fisacik")
			Case "fisacik" : Grid.CurrentCell = Row.Cells("borc")
			Case "borc" : Grid.CurrentCell = Row.Cells("alacak")
			Case "alacak" : If Row.Index = Grid.Rows.Count - 1 Then AddRow() Else Grid.CurrentCell = Grid.Rows(Row.Index + 1).Cells("hesapno")
		End Select
	End Sub

	Private Sub CheckRow(Row As DataGridViewRow)
	End Sub

	Private Sub Grid_RowValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles Grid.RowValidating
		If Not Grid.IsCurrentRowDirty Then Return
		CheckRow(Grid.Rows(e.RowIndex))
	End Sub

	Dim sube, tarih As Input

	Public Overrides Sub DialogEvent(e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Modal.Start("Şubeden aktarma", "Şube ve tarih bilgisini girin.")
				sube = Modal.AddCombo("sube", "Şube", "1,LEFKOŞA|2,GİRNE|3,MAĞUSA|4,İSKELE|5,GÜZELYURT|6,LEFKE", "width=10")
				tarih = Modal.AddDate("tarih", "Tarih", "nl", Today.YMD)
			Case EventType.Shown
				Modal.Buttons.Show("Next")
			Case EventType.Save
				Dim Rs = Muhasebe.Mahsup.SubeAktar(Remote, sube.Get, tarih.Get)
				List.DataSource = Rs
				For Each Row As DataGridViewRow In Grid.Rows
					ShowResults("hesapno", Row.Cells("hesapno").Value, Row)
				Next
				Modal.Close()
		End Select
	End Sub

End Class

